import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/pair.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';

import '../../../entity/entities/chat_new_message_entity.dart';
import '../../../res.dart';
import '../../../service/im/lf_im_service.dart';
import 'LfMessageMenu.dart';

class LfMessageImage extends StatelessWidget {
  final ChatNewMessageEntity data;
  final Function() recall;
  final Function() report;
  final Function() click;
  final bool canShowMenu;
  const LfMessageImage({
    Key? key,
    required this.data,
    required this.recall,
    required this.report,
    required this.canShowMenu,
    required this.click,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    LfAuthService authService = Get.find();
    var isSender =
        data.sender?.profile?.userId == authService.currentAccount!.userId;
    Pair pair = pairWidthAndHeight(data.width ?? ScreenUtil().screenWidth / 2,
        data.height ?? ScreenUtil().screenWidth / 2);
    Widget child;
    if ((data.localImagePath?.trim() != null &&
        (data.url?.isNullOrEmpty == true))) {
      child = RepaintBoundary(
        child: ClipRRect(
            borderRadius: BorderRadius.circular(6.r),
            child: Image.file(
              File(data.localImagePath!),
              fit: BoxFit.cover,
              width: pair.left,
              height: pair.right,
            )),
      );
    } else {
      child = CustomCacheImage(
        width: pair.left,
        height: pair.right,
        imageUrl: data.url,
        holderImagePath: Res.common_placeholder_big_photo_view,
        radius: 6.r,
      );
    }
    if (isSender) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Flexible(
              child: Container(
            padding: EdgeInsets.symmetric(
              vertical: 8.h,
            ),
            constraints:
                BoxConstraints(maxWidth: ScreenUtil().screenWidth - 70.w),
            child: getMenuWidget(
                data: data,
                child: InkWell(
                  child: child,
                  onTap: () {
                    click.call();
                  },
                ),
                isSender: isSender,
                recall: recall,
                report: report,
                reply: () {},
                canShowMenu: canShowMenu),
          )),
          10.w.sbw
        ],
      );
    } else {
      return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          10.w.sbw,
          Flexible(
              child: Container(
            padding: EdgeInsets.symmetric(
              vertical: 8.h,
            ),
            constraints:
                BoxConstraints(maxWidth: ScreenUtil().screenWidth - 70.w),
            child: getMenuWidget(
                data: data,
                child: InkWell(
                  child: child,
                  onTap: () {
                    click.call();
                  },
                ),
                isSender: isSender,
                recall: recall,
                report: report,
                reply: () {},
                canShowMenu: canShowMenu),
          )),
          10.w.sbw
        ],
      );
    }
  }
}
